package com.example.Servlet;

import com.example.Pojo.Member;
import com.example.Pojo.User;
import com.example.dao.impl.UserDaoImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/**
 * 添加会员顾客
 */
public class AddMenber extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Gson gson = new Gson();
        Member member=null;
        Map parameterMap = req.getParameterMap();

        Iterator paIter  = parameterMap.keySet().iterator();

        while (paIter.hasNext()){
            String key = paIter.next().toString();
            String[] values = (String[]) parameterMap.get(key);
            member = gson.fromJson(key,Member.class);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        member.setLatest_operating_time(sdf.format(new Date()));    //设置默认的操作时间
        //办理会员 即 客户办理 默认密码123456
        User user = new User();
        user.setId("customer");
        user.setUsername(member.getUsername());
        user.setTellphone(member.getTellphone());
        user.setPassword("123456");
        user.setRegisttime(member.getRegisttime());
        if (new UserDaoImpl().insertMeb(member) > 0 && new UserDaoImpl().saveUser(user) >0){
            System.out.println("插入成功");
        }else {
            System.out.println("插入失败");
        }
    }
}
